package com.cskaoyan.service;

import com.cskaoyan.bean.Comment;
import com.cskaoyan.bean.CommentCountVO;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.WxCommentQuery;
import com.cskaoyan.bean.comment.CommentListVO;
import com.cskaoyan.mapper.WxCommentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WxCommentServiceImpl implements WxCommentService{

    private WxCommentMapper commentMapper;

    public WxCommentServiceImpl(WxCommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    @Override
    public CommentListVO list(WxCommentQuery wxCommentQuery) {
        Integer page = wxCommentQuery.getPage();
        Integer limit = wxCommentQuery.getLimit();
//        String sort = baseParam.getSort();
//        String order = baseParam.getOrder();
        PageHelper.startPage(page, limit);
        List<Comment> commentList = commentMapper.list(wxCommentQuery);
        PageInfo<Topic> pageInfo = new PageInfo(commentList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        CommentListVO commentListVO = new CommentListVO();
        commentListVO.setTotal(total);
        commentListVO.setPages(pages);
        commentListVO.setLimit(limit);
        commentListVO.setPage(page);
        commentListVO.setList(commentList);
        return commentListVO;
    }

    @Override
    public CommentCountVO count(WxCommentQuery wxCommentQuery) {
        Integer allCount = commentMapper.countAll(wxCommentQuery);
        Integer hasPicCount = commentMapper.countHasPic(wxCommentQuery);
        CommentCountVO commentCountVO = new CommentCountVO();
        commentCountVO.setAllCount(allCount);
        commentCountVO.setHasPicCount(hasPicCount);
        return commentCountVO;
    }

    @Override
    public Integer post(Comment comment) {
        comment.setAddTime(new Date(System.currentTimeMillis()));
        comment.setUpdateTime(new Date(System.currentTimeMillis()));
        return commentMapper.post(comment);
    }
}
